package com.cspg.ssss.service;

import com.cspg.ssss.domain.CheckItem;
import com.cspg.ssss.repository.CheckItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CheckItemService {
    @Autowired
    CheckItemMapper checkItemMapper;

    public List<CheckItem> getCheckCategroy(){
        HashMap<String,Object> condition = new HashMap<String,Object>();
        condition.put("level",1);
        return checkItemMapper.selectCheckItem(condition);
    }

    public List<CheckItem> getMatters(String eqid){
        HashMap<String,Object> condition = new HashMap<String,Object>();
        condition.put("eqid",eqid);
        return checkItemMapper.selectCheckItem(condition);
    }

    public List<CheckItem> getCheckItemDetailTree(String pid){
        HashMap<String,Object> condition = new HashMap<String,Object>();
        condition.put("id",pid);
        condition.put("filter_add","0");
        return checkItemMapper.selectCheckItem(condition);
    }

    public List<CheckItem> getAddScoreCategory(){
        HashMap<String,Object> condition = new HashMap<String,Object>();
        condition.put("is_add","1");
        List<CheckItem> addScoreList = checkItemMapper.selectCheckItem(condition);
        List<String> ids = new ArrayList<String>();
        for(int i=0;i<addScoreList.size();i++){
            String categoryId = addScoreList.get(i).getId().split("\\.")[0];
            ids.add(categoryId);
        }

        condition.clear();
        condition.put("ids",ids);
        List<CheckItem> retls = checkItemMapper.selectCheckItem(condition);
        return retls;
    }

    public List<CheckItem> getAddScoreCheckItemDetailTree(String pid){
        HashMap<String,Object> condition = new HashMap<String,Object>();
        condition.put("id",pid);
        condition.put("is_add","1");
        List<CheckItem> addScoreList = checkItemMapper.selectCheckItem(condition);
        List<String> ids = new ArrayList<String>();
        for(int i=0;i<addScoreList.size();i++){
            String strId = addScoreList.get(i).getId();
            int pos = strId.lastIndexOf(".");
            String pid_level2 = strId.substring(0,pos);
            boolean bAdd = true;
            for(int j=0;j<ids.size();j++){
                if(ids.get(j).equals(pid_level2)){
                    bAdd = false;
                    break;
                }
            }
            if(bAdd){
                ids.add(pid_level2);
            }

        }

        condition.clear();
        condition.put("ids",ids);
        List<CheckItem> retls = checkItemMapper.selectCheckItem(condition);
        for(int i=0;i<addScoreList.size();i++){
            retls.add(addScoreList.get(i));
        }

        return retls;
    }

}
